package com.colin.git;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.colin.git.entity.MrCreateDTO;
import com.colin.git.entity.MrInfo;

import java.util.List;
import java.util.Optional;

public class MrRequestService {
    /**
     * @return
     */
    public static Object[][] queryMrInfo(String privateToken) {
        String string = HttpUtil.get(GitConstant.REQUEST_URL + String.format(GitConstant.MERGER_REQUEST, 35) + "?state=all&private_token=" + privateToken);
        List<MrInfo> mrInfos = JSONArray.parseArray(string, MrInfo.class);
        return mrInfos.stream().map(e -> {
            String[] strings = new String[9];
            strings[0] = e.getTitle();
            strings[1] = e.getCreatedAt();
            strings[2] = e.getState();
            strings[3] = Optional.ofNullable(e.getAuthor()).orElse(new MrInfo.AuthorDTO()).getName();
            strings[4] = e.getSourceBranch();
            strings[5] = e.getTargetBranch();
            strings[6] = Optional.ofNullable(e.getMergedBy()).orElse(new MrInfo.MergedByDTO()).getName();
            strings[7] = e.getUpdatedAt();
            strings[8] = e.getWebUrl();
            return strings;
        }).toArray(String[][]::new);
    }

    public static String createMr(MrCreateDTO mrCreateDTO, String privateToken) {
        return HttpUtil.post(GitConstant.REQUEST_URL + String.format(GitConstant.CREATE_MR, 35) + "?private_token=" + privateToken, JSONObject.toJSONString(mrCreateDTO));
    }
}

